import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:provider/provider.dart';
import 'l10n/app_localizations.dart';
import 'model/post_data.dart';
import 'dart:io';
import 'listview/ListViewDemo.dart';
import 'widget_ui/drawer_right_ui.dart';
import 'widget_ui/bottomNavigation_bar_nv.dart';
import 'widget_ui/basic_demo.dart';
import 'widget_ui/layout_demo.dart';
import 'widget_ui/view_demo.dart';
import 'widget_ui/slider_bar_demo.dart';
import "package:dio/dio.dart";
import 'login.dart';
import 'package:flutter_mvp/LocaleProvider.dart';
import 'package:provider/provider.dart';
void main() => runApp(
  ChangeNotifierProvider(
    create: (_) => LocaleProvider(),
    child: const MyApp(),
  ),
);

class MyHttpOverrides extends HttpOverrides {
  @override
  HttpClient createHttpClient(SecurityContext? context) {
    return super.createHttpClient(context)
      ..badCertificateCallback =
          (X509Certificate cert, String host, int port) => true;
  }
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    HttpOverrides.global = MyHttpOverrides();
    //语言监听
    final localeProvider = Provider.of<LocaleProvider>(context);
    return MaterialApp(
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        AppLocalizations.delegate,
      ],
      supportedLocales: [
        const Locale('en', 'US'), // English
        const Locale('zh', 'CN'), // Chinese
      ],
      locale: localeProvider.locale,
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      home: Login(),
      theme: ThemeData(
        primarySwatch: Colors.yellow,
        appBarTheme: AppBarTheme(centerTitle: true), //标题居中显示
      ),
    );
  }

}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 4,
      child: Scaffold(
        backgroundColor: Colors.grey[90],
        appBar: AppBar(
          title: Text("Hello World"),
          actions: <Widget>[
            IconButton(
              onPressed: () => debugPrint("search is pressed"),
              icon: Icon(Icons.search),
              tooltip: "search",
            ),
          ],
          elevation: 0.0,
          bottom: TabBar(
            unselectedLabelColor: Colors.black38,
            indicatorColor: Colors.black54,
            indicatorSize: TabBarIndicatorSize.label,
            indicatorWeight: 1.0,
            tabs: <Widget>[
              Tab(icon: Icon(Icons.local_florist)),
              Tab(icon: Icon(Icons.change_history)),
              Tab(icon: Icon(Icons.directions_bike)),
              Tab(icon: Icon(Icons.email)),
            ],
          ),
        ),
        body: TabBarView(
          children: <Widget>[
            ListViewDemo(),
            BasicDemo(),
            ViewDemo(),
            SliderBarDemo(),
          ],
        ),
        drawer: DrawerRightUI(),
        bottomNavigationBar: BottomNavigationBarNv(),
      ),
    );
  }
}
